const {
  CommandInteraction,
  MessageEmbed,
  MessageActionRow,
  MessageButton,
} = require('discord.js');
const DB = require('../../Structures/Schemas/suggestDB.js');

module.exports = {
  name: 'suggests',
  description: 'Create a suggestion in an organized matter',
  permission: 'ADMINISTRATOR',
  options: [
    {
      name: 'type',
      description: 'Select an option',
      type: 'STRING',
      required: true,
      choices: [
        {
          name: 'Command',
          value: 'Command',
        },
        {
          name: 'Event Listener',
          value: 'Event Listener',
        },
        {
          name: 'System',
          value: 'System',
        },
        {
          name: 'Other',
          value: 'Other',
        },
      ],
    },
    {
      name: 'suggestion',
      description: 'Describe your suggestion',
      type: 'STRING',
      required: true,
    },
  ],
  /**
   *
   * @param {CommandInteraction} interaction
   */
  async execute(interaction) {
    const { options, guildId, member, user } = interaction;
    const type = options.getString('type');
    const suggestion = options.getString('suggestion');

    const embed = new MessageEmbed()
      .setColor('NAVY')
      .setAuthor({
        name: user.tag,
        iconURL: user.displayAvatarURL({ dynamic: true, size: 512 }),
      })
      .addFields(
        {
          name: 'Suggestion',
          value: suggestion,
          inline: false,
        },
        {
          name: 'Type',
          value: type,
          inline: true,
        },
        {
          name: 'Status',
          value: 'Pending',
          inline: true,
        }
      )
      .setTimestamp();

    const buttons = new MessageActionRow();
    buttons.addComponents(
      new MessageButton()
        .setCustomId('suggest-accept')
        .setLabel('✅ Accept')
        .setStyle('PRIMARY'),
      new MessageButton()
        .setCustomId('suggest-decline')
        .setLabel('⛔ Decline')
        .setStyle('SECONDARY')
    );

    try {
      const msg = await interaction.reply({
        embeds: [embed],
        components: [buttons],
        fetchReply: true,
      });

      await DB.create({
        GuildID: guildId,
        MessageID: msg.id,
        Details: [{ MemberID: member.id, Type: type, Suggestion: suggestion }],
      });
    } catch (err) {
      console.log(err);
    }
  },
};
